package com.weetop.akly.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.weetop.akly.R;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.bean.TrainBean;
import com.weetop.akly.bean.TrainResult;
import com.weetop.akly.presenter.TrainDetailPresent;
import com.weetop.akly.view.TrainDetailView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TrainDetailActivity extends BaseActivity<TrainDetailPresent> implements TrainDetailView {

    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_data_title)
    TextView tvDataTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_exam_allscore)
    TextView tvExamAllscore;
    @BindView(R.id.tv_train_time)
    TextView tvTrainTime;
    @BindView(R.id.tv_exam_time)
    TextView tvExamTime;
    @BindView(R.id.tv_exam_question)
    TextView tvExamQuestion;
    @BindView(R.id.btn_chexiao)
    Button btnChexiao;
    @BindView(R.id.btn_bianji)
    Button btnBianji;
    @BindView(R.id.rv_option)
    RecyclerView rvOption;
    @BindView(R.id.tv_score)
    TextView tvScore;
    @BindView(R.id.tv_signtime)
    TextView tvSigntime;
    @BindView(R.id.tv_examtime)
    TextView tvExamtime;
    @BindView(R.id.ll_result)
    LinearLayout llResult;
    private String trainCode;
    private TrainBean trainInfo;
    private List<TrainBean.OptionsBean> options;
    private List<TrainBean.OptionsBean> selectOptions = new ArrayList<>();
    private CommonRecyclerAdapter<TrainBean.OptionsBean> optionAdapter;
    private boolean isResult;
    private TrainResult trainResult;
    private List<TrainResult.OptionsBean> resultOptions;
    private CommonRecyclerAdapter<TrainResult.OptionsBean> resultOptionAdapter;

    @Override
    protected TrainDetailPresent createPresenter() {
        return new TrainDetailPresent(this, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_detail);
        ButterKnife.bind(this);
        setTitleInfo("培训详情");
        Intent intent = getIntent();
        trainCode = intent.getStringExtra("train_code");
        isResult = intent.getBooleanExtra("is_result", false);
        if (isResult) {
            llResult.setVisibility(View.VISIBLE);
            llBottom.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(trainCode)) {
                mPresenter.getTrainResult(trainCode);
            }
        } else {
            if (!TextUtils.isEmpty(trainCode)) {
                mPresenter.getTrainInfo(trainCode);
            }
        }
    }

    @Override
    public void getTrainInfoSuccess(BaseModel<TrainBean> o) {
        trainInfo = o.getData();
        if (trainInfo == null) return;

        TrainBean.InfoBean info = trainInfo.getInfo();
        tvTitle.setText("培训标题：" + info.getTitle());
        tvCode.setText("培训编号：" + info.getCode());
        tvExamAllscore.setText("培训总分：" + info.getAll_score());
        tvTrainTime.setText("培训时间：" + info.getTrain_start_time() + "～" + info.getTrain_end_time());
        tvExamTime.setText("考试时间：" + info.getExam_start_time() + "～" + info.getExam_end_time());
        tvExamQuestion.setText("考试问题：" + info.getQuestion());
        tvDesc.setText("问题描述：" + info.getDescription());

        renderOptions();

        btnBianji.setText("提交答案");
    }

    private void renderOptions() {
        options = trainInfo.getOptions();
        rvOption.setLayoutManager(new LinearLayoutManager(mActivity));
        optionAdapter = new CommonRecyclerAdapter<TrainBean.OptionsBean>(mActivity, R.layout.item_option, options) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, TrainBean.OptionsBean item, int position) {
                helper.setText(R.id.tv_text, item.getOption());
            }
        };
        rvOption.setAdapter(optionAdapter);
        optionAdapter.setOnItemClickListener((viewHolder, view, position) -> {
            TrainBean.OptionsBean option = options.get(position);
            TextView tvText = viewHolder.itemView.findViewById(R.id.tv_text);
            RelativeLayout rlItem = viewHolder.itemView.findViewById(R.id.rl_item);
            if (selectOptions.contains(option)) {
                tvText.setTextColor(getColor(R.color.black));
                rlItem.setBackgroundResource(R.drawable.bg_state_normal);
                selectOptions.remove(option);
            } else {
                tvText.setTextColor(getColor(R.color.white));
                rlItem.setBackgroundResource(R.drawable.bg_state_false);
                selectOptions.add(option);
            }
        });
    }

    @Override
    public void getTrainResultSuccess(BaseModel<TrainResult> o) {
        trainResult = o.getData();
        if (trainResult == null) return;

        TrainResult.InfoBean info = trainResult.getInfo();
        tvTitle.setText("培训标题：" + info.getTitle());
        tvCode.setText("培训编号：" + info.getCode());
        tvExamAllscore.setText("培训总分：" + info.getAll_score());
        tvTrainTime.setText("培训时间：" + info.getTrain_start_time() + "～" + info.getTrain_end_time());
        tvExamTime.setText("考试时间：" + info.getExam_start_time() + "～" + info.getExam_end_time());
        tvExamQuestion.setText("考试问题：" + info.getQuestion());
        tvDesc.setText("问题描述：" + info.getDescription());

        TrainResult.UserBean user = trainResult.getUser();
        tvScore.setText("本次得分：" + user.getExam_score());
        tvSigntime.setText("签到时间：" + user.getSign_time());
        tvExamtime.setText("考试时间：" + user.getExam_time());

        renderResultOptions();

    }

    private void renderResultOptions() {
        resultOptions = trainResult.getOptions();
        rvOption.setLayoutManager(new LinearLayoutManager(mActivity));
        resultOptionAdapter = new CommonRecyclerAdapter<TrainResult.OptionsBean>(mActivity, R.layout.item_option, resultOptions) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, TrainResult.OptionsBean item, int position) {
                helper.setText(R.id.tv_text, item.getOption());
                if (item.getIs_true() == 1) {//正确
                    helper.setTextColorRes(R.id.tv_text, R.color.white);
                    helper.setBackgroundRes(R.id.rl_item, R.drawable.bg_state_true);
                    helper.setVisible(R.id.iv_status, true);
                    helper.setImageResource(R.id.iv_status, R.drawable.ic_true);
                } else {//错误
                    helper.setTextColorRes(R.id.tv_text, R.color.white);
                    helper.setBackgroundRes(R.id.rl_item, R.drawable.bg_state_false);
                    helper.setVisible(R.id.iv_status, true);
                    helper.setImageResource(R.id.iv_status, R.drawable.ic_false);
                }
            }
        };
        rvOption.setAdapter(resultOptionAdapter);
    }

    @Override
    public void doTrainSuccess(BaseModel<Boolean> o) {
        if (o.getData()) {
            ToastUtils.showShort("提交成功");
            Intent intent = new Intent();
            intent.putExtra("is_refresh", true);
            this.setResult(RESULT_OK, intent);
            finish();
        } else {
            ToastUtils.showShort(o.getMsg());
        }
    }

    @OnClick({R.id.btn_chexiao, R.id.btn_bianji})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_chexiao:
                break;
            case R.id.btn_bianji:
                if (selectOptions.size() > 0) {
                    String ids = "";
                    for (TrainBean.OptionsBean option : selectOptions) {
                        if (TextUtils.isEmpty(ids)) {
                            ids = option.getId() + "";
                        } else {
                            ids += "," + option.getId();
                        }
                    }
                    mPresenter.doTrain(trainCode, ids);
                } else {
                    ToastUtils.showShort("请选择选项");
                }
                break;
        }
    }
}
